package top.infopub.mgr.mgc.web;


import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import top.infopub.generic.GenericController;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.ErrorCodeEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mgc.domain.TemplateTypeDomain;
import top.infopub.mgr.mgc.service.TemplateTypeService;
import top.infopub.model.mgc.TemplateType;
import top.infopub.security.util.SessionUserUtils;

import com.github.pagehelper.Page;


/**
 * 消息模板分类管理控制层
 * @author Awoke
 * @version 2018年11月8日
 * @see MsgTemplateTypeController
 * @since
 */
@Controller
@RequestMapping("/msgTemplateType")
public class MsgTemplateTypeController extends GenericController {

    @Autowired
    private TemplateTypeService templateTypeService;

    /**
     * 进入消息模板分类管理
     * @return
     */
    @RequestMapping("/list")
    public String list() {
        return "mgc/laymsg-template-type-list";
    }

    /**
     * 查询消息模板分类节点
     * @return
     */
    @RequestMapping("/query")
    public @ResponseBody RspVo<TemplateTypeDomain> query(TemplateType bean,
                                                              ParamPage paramPage) {
        Page<TemplateTypeDomain> page = new Page<TemplateTypeDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        Page<TemplateTypeDomain> pageList = templateTypeService.queryPageByDomainWhere(page, bean);
        return RspVo.getSuccessResponse(pageList.getTotal(), pageList.getResult());
    }

    /**
     * 批量删除消息模板分类
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete")
    public @ResponseBody RspVo delete(String[] checkboxId) {
        templateTypeService.batchDeleteByIds(checkboxId);
        return RspVo.getSuccessResponse();
    }

    /**
     * 新增消息模板分类
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/insert")
    public @ResponseBody RspVo insert(TemplateType bean) {
        if (templateTypeService.isCodeExist(bean)) {
            return ErrorCodeEnum.ERROR_COMMON_CODE_UK.getRspVo();
        }
        bean.setAddtime(new Date());
        templateTypeService.insert(bean);
        return RspVo.getSuccessResponse();
    }

    /**
     * 修改消息模板分类
     * @param checkboxId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/update")
    public @ResponseBody RspVo update(TemplateType bean) {
        TemplateType info = templateTypeService.selectByPrimaryKey(bean.getId());
        if (info != null) {
            if (!info.getCode().equals(bean.getCode())) {
                if (templateTypeService.isCodeExist(bean)) {
                    return ErrorCodeEnum.ERROR_COMMON_CODE_UK.getRspVo();
                }
            }
        }
        bean.setUpdatetime(new Date());
        bean.setUpateUser(SessionUserUtils.getSessionUserName());
        templateTypeService.updateBySelective(bean);
        return RspVo.getSuccessResponse();
    }

}
